package com.link.base.base.delivery.controller;


import com.link.base.base.delivery.model.DeliveryDetails;
import com.link.base.base.delivery.service.DeliveryDetailsService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单管理,发货管理
 *
 * @author 黄龙建
 * @date 2020/1/8 5:39 下午
 */
@Controller
@RequestMapping("/link/deliveryDetails")
public class DeliveryDetailsController extends BasicController<DeliveryDetails> {

    @Resource
    private DeliveryDetailsService deliveryDetailsService;

    @Override
    public BasicService<DeliveryDetails> getBasicService() throws Exception {
        return deliveryDetailsService;
    }

    /**
     * <p>到货确认:<br/>1.修改发货明细到货数量
     * <br/>
     * 2.调用订单到货确认接口同步订单行的到货数量，生成记录、生成或修改库存产品-生成或修改库存产品数量、修改订单状态</p>
     *
     * @param deliveryItemList 发货明细list
     * @author 宁蓝昊
     */
    @RequestMapping(value = "/arrivedOrderItem", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> arrivedOrderItem(@RequestBody List<DeliveryDetails> deliveryItemList) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            if (CollectionUtils.isEmpty(deliveryItemList)) {
                throw new ServiceException("DELIVERY-001");
            }
            deliveryDetailsService.arrivedOrderItem(deliveryItemList);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
